package com.geese.filter.rsa;

import com.geese.common.ParamJsonModel;
import com.geese.filter.SecurityFilter;

import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;

/**
 * Created by Z先生 on 2017/2/22.
 */
public class Test {
    private static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALZrhNWismkObY3Opkjd39aiV3Dc\n" +
            "/uiDrqVhs3DpkHTaEAmWEdgRB9wh3H/fmdZMV0hFxrZ4bHoqE60v7uy9zXnQBWVa4TCcCj1S32DO\n" +
            "cV8ntfSsLHX7rTbHxaKi9wJbzJD8SLMWUl8ukjJL/nvZWufEt62FW2q6IGib2ki8Y0tFAgMBAAEC\n" +
            "gYEAjU15xrZRNpmLeRYCrlzXf32e9/jnp5ohFVA53BO/5xPm6lYJmnGOwhCkqZS9qEbYMU6DX2Km\n" +
            "vHaNaVDA5TccypVtnu8DguZ1gj2PUwDXSz9HcJEUvWfsFBSma0xy5Jqw1OfngKRaVxtPLCuBG+PQ\n" +
            "5WGnxyM47BAEc4s54UvE50ECQQD5zYWpUHmPsWJ1BPj4zmcuHFCP1y5ReDQV1QWFD0YsAPJctL9w\n" +
            "/YRa3hzg0beQrFpwbeMxPlL+w0KbtLJ/Y4u1AkEAuvINxZ0HtCFrpCuVHQbSUn3xBYH2TimiRGWO\n" +
            "vDwEEcsWTS0BHbG01LXqtmHn/2paBUQc9zWCO2Yq0XAXnogbUQJAMTskAH5vtrYK5W0ZdE+ZXikO\n" +
            "Z4rNAhliinr1H4gnGD0HhuXIZQhRsWbc4iHyv3x4Rnghsm7KBJOO6QjvtNN1kQJAOKAW8dOVwMca\n" +
            "vriAyzEExf3l4qAyBgzwXL1SVcvsA6O7VpcaLm9kccXEmYfgaOpC6DNwfqTTlMBmmdfVH6zkcQJB\n" +
            "AOtdGN67GIi7LvtRBpmkfMTUE3go9489OYGND9Z1p4RqISW9g5n89sp5uCO5P8yAet+xIZNR3Qi+\n" +
            "lUj38ks4MHU=";

    private static String  publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2a4TVorJpDm2NzqZI3d/Woldw3P7og66lYbNw\n" +
            "6ZB02hAJlhHYEQfcIdx/35nWTFdIRca2eGx6KhOtL+7svc150AVlWuEwnAo9Ut9gznFfJ7X0rCx1\n" +
            "+602x8WiovcCW8yQ/EizFlJfLpIyS/572VrnxLethVtquiBom9pIvGNLRQIDAQAB";



    public static void main(String[] args) throws Exception {
        RSAForPrivateCodec rsa = new RSAForPrivateCodec(privateKey,publicKey);

        RSAForPublicCodec PublicCodec = new RSAForPublicCodec(publicKey);

        String token = "jjjkjgkflduioe";
        String timestamp = "123456878456";
        String nonce = "jkhkgfds-=gfdfsgf+gfsgf";
        String data = "{\"username\":\"zhangsan\",\"password\":\"aaa333\"}";

        String sign = "Q1dn0Lk40IZUm4seSkd1g+yAvMZtXRjpHhlICSsOrLDwBpXIu8ys5a1zWx+FyqTdhenKwRJU26qy\n" +
                "HGGMrAJsrzi0CHuqRChbNMV81qBsdxFWWfvRVZt7gjH3QljBgYSJ5HUiPMuiL9SHRXYP8jgoV/Sy\n" +
                "MGWBEj+b15oTkzgDo8Y=";




        //生成签名
        TreeMap<String,String> params=new TreeMap<String, String>();
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("data",MD5Util.MD5Base64(data));
        params.put("token",token);

        byte[] jiemi = rsa.decrypt(Base64Utils.getInstance().decoder(sign));
        String result =new String(jiemi);

        String originSign = SecurityFilter.createSign(params);

        System.out.println( result.trim().equals(originSign.trim()));

        byte [] wmy1 = PublicCodec.encrypt(SecurityFilter.createSign(params).getBytes());
        String  wmy2 =  Base64Utils.getInstance().encoder(wmy1);
       // System.out.println(wmy2);
        params.put("sign",wmy2);


        byte[] zmm1 = Base64Utils.getInstance().decoder(params.get("sign"));
        byte [] zmm2= rsa.decrypt(zmm1);

        params.remove("sign");
        System.out.println(new String(zmm2).equals(SecurityFilter.createSign(params)));




        /*System.out.println(SecurityFilter.createSign(params));
        byte [] jiami = PublicCodec.encrypt(SecurityFilter.createSign(params).getBytes("UTF-8"));
        String sign = Base64Utils.getInstance().encoder(jiami);


        byte []  xxxxx= Base64Utils.getInstance().decoder(sign);
        byte [] yt= rsa.decrypt(xxxxx);
        System.out.println(new String(jiami).equals(new String(yt)));
        System.out.println(new String(jiami).equals(new String(xxxxx)));


        String xxx= "data=chDFcdwRUmHEUa8H7L4uuw==&nonce=jkhkgfds-=gfdfsgf+gfsgf&timestamp=123456878456&token=jjjkjgkflduioe";
        byte []temp1=PublicCodec.encrypt(xxx.getBytes());
        byte [] temp2 = rsa.decrypt(temp1);
        System.out.println("xxxxxx"+new String(temp2).equals(xxx));

        String temp3 = Base64Utils.getInstance().encoder(temp1);
        byte []temp4= Base64Utils.getInstance().decoder(temp3);
        System.out.println(new String(rsa.decrypt(temp4)).equals(xxx));
*/


        /*String token = "jjjkjgkflduioe";
        String timestamp = "123456878456";
        String nonce = "jkhkgfds-=gfdfsgf+gfsgf";
        String data = "{\"username\":\"zhangsan\",\"password\":\"aaa333\"}";

        //生成签名
        TreeMap<String,String> params=new TreeMap<String, String>();
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("data",MD5Util.MD5Base64(data));
        String tempSign = SecurityFilter.createSign(params);
        byte[] jiami = PublicCodec.encrypt(tempSign.getBytes("UTF-8"));
        String sign  = Base64Utils.getInstance().encoder(jiami);
        params.put("sign",sign);
        String result =  SecurityFilter.createSign(params);
        System.out.println(result);

        byte[] jiemi = rsa.decrypt(Base64Utils.getInstance().decoder(sign));
        //生成签名
        TreeMap<String,String> jiemiparams=new TreeMap<String, String>();
        jiemiparams.put("timestamp", timestamp);
        jiemiparams.put("nonce", nonce);
        jiemiparams.put("data",MD5Util.MD5Base64(data));
        jiemiparams.put("sign",new String(jiemi));
        String perpmar =  SecurityFilter.createSign(params);
        System.out.println(perpmar.equals(result));*/


    }
}
